import { NextApiRequest } from 'next';
import NextDocument, { DocumentContext, Head, Html, Main, NextScript } from 'next/document';
import anylogger from 'anylogger';
import { tbfdrole } from '../atoms/db';
import getuser from '../atoms/getuser';

const logger = anylogger('/_document.page');

interface Props {
}

export default class Document extends NextDocument<Props> {
	static async getInitialProps(ctx: DocumentContext) {
		const page = ctx.pathname;
		logger.log(page);
		if (ctx.req) {
			const user = await getuser(ctx.req as NextApiRequest);
			if (user) {
				logger.log('用户', user.name, '正在访问页面', page);
			} else {
				logger.log('未登录用户正在访问页面', page);
			}
		} else {
			logger.log('未登录用户正在访问页面', page);
		}
		const initialProps = await NextDocument.getInitialProps(ctx);
		return {
			...initialProps
		};
	}
	render() {
		return (
			<Html>
				<Head>
					<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"></link>
					<link rel="icon" type="image/x-icon" sizes="32x32" href="/favicon-32x32.ico" ></link>
					<link rel="icon" type="image/x-icon" sizes="16x16" href="/favicon-16x16.ico"></link>
				</Head>
				<body>
					<Main />
					<NextScript />
				</body>
			</Html>
		);
	}
}
